﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using QS.Logic.Modules;
using Microsoft.Xna.Framework;
using QS.Game.Renderer;

namespace QS.Game.Logic.Modules
{
    class QCockpitModel : QModuleModel
    {
        private VertexPositionNormalTexture[] m_GlasVertices;
        private VertexPositionNormalTexture[] m_MetalVertices;

        private short[] m_GlasIndices;
        private short[] m_MetalIndices;

        private QShipCockpit m_Module;

        public QCockpitModel(QShipCockpit module)
        {
            m_Module = module;
            Calculate();
        }

        public void Calculate()
        {
            if (m_MetalVertices == null)
                m_MetalVertices = new VertexPositionNormalTexture[36];

            if (m_MetalIndices == null)
                m_MetalIndices = new short[120];


            Vector3 trans = new Vector3(m_Module.ArrayPosition.X, m_Module.ArrayPosition.Y, m_Module.ArrayPosition.Z);

            Vector3 Size = new Vector3(1, 1, 0.5f);

            //Calculate the position of the vertices on the top face.
            Vector3 topLeftFront = new Vector3(0.0f, 1.0f, 0.0f) * Size;
            Vector3 topLeftBack = new Vector3(0.0f, 1.0f, 1.0f) * Size;
            Vector3 topRightFront = new Vector3(1.0f, 1.0f, 0.0f) * Size;
            Vector3 topRightBack = new Vector3(1.0f, 1.0f, 1.0f) * Size;

            // Calculate the position of the vertices on the bottom face.
            Vector3 btmLeftFront = new Vector3(0.0f, 0.0f, 0.0f) * Size;
            Vector3 btmLeftBack = new Vector3(0.0f, 0.0f, 1.0f) * Size;
            Vector3 btmRightFront = new Vector3(1.0f, 0.0f, 0.0f) * Size;
            Vector3 btmRightBack = new Vector3(1.0f, 0.0f, 1.0f) * Size;

            // Normal vectors for each face (needed for lighting / display)
            Vector3 normalFront = new Vector3(0.0f, 0.0f, 1.0f) * Size;
            Vector3 normalBack = new Vector3(0.0f, 0.0f, 0.0f) * Size;
            Vector3 normalTop = new Vector3(0.0f, 1.0f, 0.0f) * Size;
            Vector3 normalBottom = new Vector3(0.0f, 0.0f, 0.0f) * Size;
            Vector3 normalLeft = new Vector3(0.0f, 0.0f, 0.0f) * Size;
            Vector3 normalRight = new Vector3(1.0f, 0.0f, 0.0f) * Size;

            // UV texture coordinates
            Vector2 textureTopLeft = new Vector2(1.0f * Size.X, 0.0f * Size.Y);
            Vector2 textureTopRight = new Vector2(0.0f * Size.X, 0.0f * Size.Y);
            Vector2 textureBottomLeft = new Vector2(1.0f * Size.X, 1.0f * Size.Y);
            Vector2 textureBottomRight = new Vector2(0.0f * Size.X, 1.0f * Size.Y);

            // Add the vertices for the FRONT face.
            m_MetalVertices[0] = new VertexPositionNormalTexture(topLeftFront, normalFront, textureTopLeft);
            m_MetalVertices[1] = new VertexPositionNormalTexture(btmLeftFront, normalFront, textureBottomLeft);
            m_MetalVertices[2] = new VertexPositionNormalTexture(topRightFront, normalFront, textureTopRight);
            m_MetalVertices[3] = new VertexPositionNormalTexture(btmLeftFront, normalFront, textureBottomLeft);
            m_MetalVertices[4] = new VertexPositionNormalTexture(btmRightFront, normalFront, textureBottomRight);
            m_MetalVertices[5] = new VertexPositionNormalTexture(topRightFront, normalFront, textureTopRight);

            // Add the vertices for the BACK face.
            m_MetalVertices[6] = new VertexPositionNormalTexture(topLeftBack, normalBack, textureTopRight);
            m_MetalVertices[7] = new VertexPositionNormalTexture(topRightBack, normalBack, textureTopLeft);
            m_MetalVertices[8] = new VertexPositionNormalTexture(btmLeftBack, normalBack, textureBottomRight);
            m_MetalVertices[9] = new VertexPositionNormalTexture(btmLeftBack, normalBack, textureBottomRight);
            m_MetalVertices[10] = new VertexPositionNormalTexture(topRightBack, normalBack, textureTopLeft);
            m_MetalVertices[11] = new VertexPositionNormalTexture(btmRightBack, normalBack, textureBottomLeft);

            // Add the vertices for the TOP face.
            m_MetalVertices[12] = new VertexPositionNormalTexture(topLeftFront, normalTop, textureBottomLeft);
            m_MetalVertices[13] = new VertexPositionNormalTexture(topRightBack, normalTop, textureTopRight);
            m_MetalVertices[14] = new VertexPositionNormalTexture(topLeftBack, normalTop, textureTopLeft);
            m_MetalVertices[15] = new VertexPositionNormalTexture(topLeftFront, normalTop, textureBottomLeft);
            m_MetalVertices[16] = new VertexPositionNormalTexture(topRightFront, normalTop, textureBottomRight);
            m_MetalVertices[17] = new VertexPositionNormalTexture(topRightBack, normalTop, textureTopRight);

            // Add the vertices for the BOTTOM face. 
            m_MetalVertices[18] = new VertexPositionNormalTexture(btmLeftFront, normalBottom, textureTopLeft);
            m_MetalVertices[19] = new VertexPositionNormalTexture(btmLeftBack, normalBottom, textureBottomLeft);
            m_MetalVertices[20] = new VertexPositionNormalTexture(btmRightBack, normalBottom, textureBottomRight);
            m_MetalVertices[21] = new VertexPositionNormalTexture(btmLeftFront, normalBottom, textureTopLeft);
            m_MetalVertices[22] = new VertexPositionNormalTexture(btmRightBack, normalBottom, textureBottomRight);
            m_MetalVertices[23] = new VertexPositionNormalTexture(btmRightFront, normalBottom, textureTopRight);

            // Add the vertices for the LEFT face.
            m_MetalVertices[24] = new VertexPositionNormalTexture(topLeftFront, normalLeft, textureTopRight);
            m_MetalVertices[25] = new VertexPositionNormalTexture(btmLeftBack, normalLeft, textureBottomLeft);
            m_MetalVertices[26] = new VertexPositionNormalTexture(btmLeftFront, normalLeft, textureBottomRight);
            m_MetalVertices[27] = new VertexPositionNormalTexture(topLeftBack, normalLeft, textureTopLeft);
            m_MetalVertices[28] = new VertexPositionNormalTexture(btmLeftBack, normalLeft, textureBottomLeft);
            m_MetalVertices[29] = new VertexPositionNormalTexture(topLeftFront, normalLeft, textureTopRight);

            // Add the vertices for the RIGHT face. 
            m_MetalVertices[30] = new VertexPositionNormalTexture(topRightFront, normalRight, textureTopLeft);
            m_MetalVertices[31] = new VertexPositionNormalTexture(btmRightFront, normalRight, textureBottomLeft);
            m_MetalVertices[32] = new VertexPositionNormalTexture(btmRightBack, normalRight, textureBottomRight);
            m_MetalVertices[33] = new VertexPositionNormalTexture(topRightBack, normalRight, textureTopRight);
            m_MetalVertices[34] = new VertexPositionNormalTexture(topRightFront, normalRight, textureTopLeft);
            m_MetalVertices[35] = new VertexPositionNormalTexture(btmRightBack, normalRight, textureBottomRight);

            for (int i = 0; i < 36; i++)
                m_MetalIndices[i] = (short)i;

            

            for(int i = 0; i< m_MetalVertices.Length; i++)
            {
                m_MetalVertices[i].Position += trans;
            }
        }

        public override void Draw(QCamera camera)
        {
            QRendererCollection.GlassRenderer.Draw(m_MetalVertices, m_MetalIndices, camera);
        }

    }
}
